<?php 	
include_once("../helper/DBOperation.php");
$dbObj = new DBOperation();

include("../helper/check_user_login.php");
checkUserIsLogin("admin");

include_once("includes/function.php");

if($_POST){
	$new_password  = $_POST['new_password'];
	$confirm_password  = $_POST['confirm_password'];
	$id = $_POST['id'];
	$error ="";
	$success ="";

	
	if($new_password==""){
		$error.= "New Password can not be left blank. <BR>";
	}

	if($confirm_password=="" || $confirm_password!=$new_password){
		$error.= "Password and Confirm Password does not match. <BR>";
	}
	
	if($error==""){
		$new_password = mysql_real_escape_string($new_password);
		$id = mysql_real_escape_string($id);
		
		$query = "Update tbl_users set password='".md5($new_password)."' where id='".$id."'";
			if(mysql_query($query)){
				$_SESSION['success'] = "Password has been changed successfully.";
				header("location:user_listing.php");
			}else{
				$error = "Password can not be changed.";
			}						
	}
	
}

if(isset($_REQUEST['id']) && $_REQUEST['id']!=""){
	$id = $_REQUEST['id'];
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Welcome to Admin Panel</title>
	<link href="css/style.css" rel="stylesheet" type="text/css" />
	<script language="JavaScript" src="js/jquery.js"></script>
	<script language="JavaScript" src="js/jquery.validate.js"></script>
</head>
<body>
<div id="container">
	<div id="navigation">
		<div id="title">Change Password</div>
		<div id="nav"><a href="../unit_a.php">Go To Mobile Site</a> | <a href="user_listing.php">User Listing</a> | <a href="logout.php">Logout</a></div>
	</div>
	<div id="body">
		<div id="user_listing">
			<div id="error"><?php if(isset($error) && $error!="") { echo $error; }?></div>
			<form name="webform" id="webform" action="change_password.php?id=<?php echo $id;?>" method="post">
				<table id="add_user">
					<tr>
						<td id="lbl">New Password <span id="required"> * </span>: </td>
						<td><input type='password' id="new_password" name="new_password" value="" class="text_field_bg required password"/></td>
					</tr>
					<tr>
						<td id="lbl">Confirm Password <span id="required"> * </span>: </td>
						<td><input type='password' id="confirm_password" name="confirm_password" value="" class="text_field_bg"/></td>
					</tr>
					<tr>
						<td><input type="hidden" id="user_id" name="id" value="<?php echo $id;?>"></td>
						<td><input type='submit' id="submit" name="submit" value="Submit" /></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</div>

</body>
<script>
	$(document).ready(function(){
		
		$("#webform").validate({
			  rules: {
				'new_password': "required",
				'confirm_password': {
				  equalTo: "#new_password"
				}
			  }
		});
	});   
</script>
</html>
